import time

import allure
from selenium.webdriver.common.by import By

from bazhuayun.page.base_octopus_cloud import BaseOctopusCloud
from bazhuayun.utils.log_utils import logger


class ChannelAddPage(BaseOctopusCloud):
    _CHANNEL = By.XPATH, '//*[text()="渠道(群)"]'
    _CHANNEL_MANAGE = By.XPATH, '//*[text()="渠道管理"]'
    _CHANNEL_ADD = By.XPATH, '//*[text()="添加渠道"]'
    _INPUT_NAME = By.XPATH, '//*[@class="el-input__inner"]'
    _CLICK_FOUND = By.XPATH, '//*[text()="查询"]'
    _CLICK_FOUND_NAME = By.XPATH, '(//*[@class="cell"])[68]'
    _DELETE = By.XPATH, '//*[@class="el-button amt2 el-button--danger el-button--mini"]'
    _DELETE_OK = By.CSS_SELECTOR, ".el-button.el-button--default.el-button--small.el-button--primary"
    _DELETE_SUCCESS = By.XPATH, '//*[text()="删除成功"]'

    @allure.step("进入添加渠道页面")
    def channel_add(self):
        # 渠道(群)
        self.find_ele(*self._CHANNEL).click()
        # 渠道管理
        self.find_ele(*self._CHANNEL_MANAGE).click()
        # 添加渠道
        time.sleep(3)
        self.find_ele(*self._CHANNEL_ADD).click()
        logger.info("进入添加渠道页面")

        from bazhuayun.page.channel_page import ChannelPage
        return ChannelPage(self.driver)

    @allure.step("获取创建的渠道")
    def get_channel(self, name):
        # 名称输入框
        self.find_ele(*self._INPUT_NAME).click()
        self.find_ele(*self._INPUT_NAME).send_keys(name)
        # 点击查询
        self.find_ele(*self._CLICK_FOUND).click()
        # 定位群名称
        res = self.find_ele(*self._CLICK_FOUND_NAME)
        logger.info("查找添加的渠道")
        return res

    @allure.step("删除创建的渠道")
    def delete_channel(self, name):
        # 渠道(群)
        self.find_ele(*self._CHANNEL).click()
        # 渠道管理
        self.find_ele(*self._CHANNEL_MANAGE).click()
        # 名称输入框
        self.find_ele(*self._INPUT_NAME).click()
        self.find_ele(*self._INPUT_NAME).send_keys(name)
        # 点击查询
        self.find_ele(*self._CLICK_FOUND).click()
        # 删除
        time.sleep(2)
        self.delete_channel_qiangzhi(*self._DELETE)
        # 确认删除
        self.find_ele(*self._DELETE_OK).click()
        # 定位删除成功
        time.sleep(2)
        res = self.find_ele(*self._DELETE_SUCCESS)
        logger.info("删除添加的渠道")
        return res





